package com.borya.task.impl;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.borya.task.AbstractTaskService;
import com.borya.util.Constant;
import com.borya.util.DateUtils;

public class FallPastImg extends AbstractTaskService{
	private Logger log = Logger.getLogger(getClass());
	public FallPastImg() {
		//指定每日更新时间，开启后没一天执行一次
		super(Constant.IMAGE_AUTOREMOVE_TIME);
	}

	@Override
	public void process() {
		String nowTime = DateUtils.format(System.currentTimeMillis(), "yyyyMMdd HH:mm:ss");
		for (String eFile : destFiles) {
			final String destUpFile = Constant.IMG_SAVE_PATH+File.separator+eFile;
			scanFile(destUpFile);
			log.info("当前时间："+nowTime+",清理路径："+destUpFile+"所有文件");
		}
	}
	private String[] destFiles=new String[]{"registing","sign"};
	private static void scanFile(String filePath){
		File file = new File(filePath);
		if(!file.isFile()){
			String[] list = file.list();
			for (String subFile : list) {
				scanFile(filePath+File.separator+subFile);
			}
		}else{
			long now = System.currentTimeMillis();
			if(file.lastModified()-now>TimeUnit.DAYS.toMillis(1)){
				file.delete();
			}
		}
	}
}
